package com.moter.demo01.juc.component;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * 减法计数器
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        //本质是一个计数器
        CountDownLatch countDownLatch = new CountDownLatch(5);//参数是初始计数的大小
        for (int i = 0; i < 4; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+" A;rea Go Out");
                countDownLatch.countDown();//-1
                System.out.println(countDownLatch.getCount());
            }).start();
        }
        //会阻塞，一直等到计数器结束,await表示是否计数完毕(带参数的)
        boolean await = countDownLatch.await(5, TimeUnit.SECONDS);
//        countDownLatch.await();
        System.out.println(await);
        if(!await){
            System.out.println("somebody locked in thr room forever");
        }
        System.out.println("Close Door");
    }
}
